package com.bac.bacplatform;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.Response.Listener;
import com.bac.utils.Config;
import com.bac.utils.Util;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class PayPwdActivity extends BaseActivity implements OnClickListener{
	private Button mButton;
	private EditText idCardNum;
	private String idCradNumString;
	private ImageButton clear_idcard_number;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.paypassword_back);
		findViewById(R.id.title_left).setOnClickListener(this);

	}

	@Override
	public void init() {
		idCardNum = (EditText) findViewById(R.id.idcard_number);
		
		mButton = (Button) findViewById(R.id.confirm_next_paypwd);
		mButton.setOnClickListener(this);
		
		clear_idcard_number = (ImageButton) findViewById(R.id.btn_clear_idcard_number);
		clear_idcard_number.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				idCardNum.setText("");
			}
		});
		idCardNum.addTextChangedListener(watcher);
		idCardNum.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					if (idCardNum.getText().toString() != null 
							&& !("").equals(idCardNum.getText().toString())) {
						clear_idcard_number.setVisibility(View.VISIBLE);
					}else {
						clear_idcard_number.setVisibility(View.INVISIBLE);
					}
				}
			}
		});
	}
	
	TextWatcher watcher = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub
		}
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub
		}
		
		@Override
		public void afterTextChanged(Editable s) {
			if (idCardNum.hasFocus() 
					&& idCardNum.getText().toString().trim() != null 
					&& !("").equals(idCardNum.getText().toString().trim())) {
				clear_idcard_number.setVisibility(View.VISIBLE);
			}else {
				clear_idcard_number.setVisibility(View.INVISIBLE);
			}
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.confirm_next_paypwd:
			CollectIdNum();
			break;
		case R.id.title_left:
			finishActivityOut();
			break;
		default:
			break;
		}
		
	}
	
	/**
	 * 获取修改支付密码人的身份证号
	 */
	private void CollectIdNum() {
		/**
		 * 成功返回结果
		 */
		Listener<String> sl = new Listener<String>() {
			@Override
			public void onResponse(String response) {
				dismissLoadingProgress();
				Log.i("TAG----success",response.toString());
				
				Intent intentToPayPwdPhone = new Intent(PayPwdActivity.this,PayPwdPhoneActivity.class);
				startActivity(intentToPayPwdPhone);
			}
		};
		
		idCradNumString = idCardNum.getText().toString().trim();
		if (idCradNumString.length() <= 0) {
			showToast(R.string.input_idcardnum);
			return;
		}
		showLoadingProgress();
		String path = Config.URL_COLLECT_IDCARD_NUMBER;
		Map<String, String> map = new HashMap<String, String>();
		map.put("idCardNo",idCradNumString);
		Util.httpStringPost(path, sl, this, map);
	}

}
